<?php
class User_Form_UserAdd extends Promotor_Form_Scaffold {
	public function init() {
		$this->setMethod('post');
		$this->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);

		$this->addPrefixPath('Promotor_Form_Decorator_','Promotor/Form/Decorator', self::DECORATOR);

		$subForm = $this->_getDefaultForm();
        $this->addSubForm($subForm, 'defaulttab');
        
        $subForm = $this->_getExForm();
        $this->addSubForm($subForm, 'exttab');
	}
    
    /**
     * @return Zend_Dojo_Form_SubForm
     */
    protected function _getDefaultForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'defaulttab',
            'legend' => 'Opcje główne'
        ));
        $subForm
        	->addElement(
                'text',
                'username',
                array(
                    'label' => 'Nazwa użytkownika',
                    'required'  => true,
                	'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim()
                	),
                	'validators' => array(
                		'db_no_exsists' => new Zend_Validate_Db_NoRecordExists('site','alias')
                	)
                )
            )
            ->addElement(
                'text',
                'email',
                array(
                    'label'      => 'Email',
                	'required'  => true,
                    'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim()
                	),
                	'validators' => array(
                		'email' => new Zend_Validate_EmailAddress(),
                		'strlen' => new Zend_Validate_StringLength(4),
                		'db_no_exsists' => new Zend_Validate_Db_NoRecordExists('user','email')
                	)
				)
            )
            ->addElement(
                'Password',
                'password',
                array(
                    'label'      => 'Hasło',
	                'required'  => true,
                    'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim()
                	),
                	'validators' => array(
                		'strlen' => new Zend_Validate_StringLength(4),
                		'strlen' => new Zend_Validate_Identical(@$_POST['defaulttab']['repassword'])
                	)
				)
            )
            ->addElement(
                'Password',
                'repassword',
                array(
                    'label'      => 'Powtórz hasło',
                	'ignore' 	 => true,
                    'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim()
                	),
				)
            )
            ->addElement(
                'text',
                'role',
                array(
                    'label'      => 'Rola',
                	'required'  => true,
                    'trim'       => true,
                	'filters' => array(
                		'strtrim' => new Zend_Filter_StringTrim(),
                		'rewrite' => new KontorX_Filter_Word_Rewrite()
                	),
                	'validators' => array(
                		'strlen' => new Zend_Validate_StringLength(4)
                	)
				)
            )
           ->addElement(new KontorX_Form_Element_File('image', array(
        		'label' => 'Zdięcie',
        		'required' => false,
        		'destination' => User_Model_User::UPLOAD_PATH,
        		'filters' => array(
        			'UniqName' => new KontorX_Filter_File_UniqName()
        		),
        		'validators' => array(
        			'upload' => array(
        				'validator' => 'Upload'),
        			'isImage' => array(
        				'validator' => 'IsImage'),
        			'size' => array(
        				'validator' => 'Size',
        				'options' => array(
        					'min' => '3kb',
        					'bytestring' => true)),
        			'count' => array(
        				'validator' => 'Count',
        				'options' => array(
        					'max' => 1))
        		)
        	)));
            
        return $subForm;
    }

	/**
     * @return Zend_Form_SubForm
     */
    protected function _getExForm() {
    	$subForm = new Zend_Form_SubForm();
        $subForm->setAttribs(array(
            'name'   => 'exttab',
            'legend' => 'Zaawansowane'
        ));
        $subForm
            ->addElement(
            	'checkbox',
            	'registered',
            	array(
            		'label' => 'Zarejestrowany',
            		'value' => 1,
            		'checked' => true
            	)
            );
            
        return $subForm;
    }
}